//
//  ArticleViewController.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/20.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "ArticleViewController.h"
#import "ArticleCollectionView.h"

@interface ArticleViewController ()

@end

@implementation ArticleViewController {
    ArticleCollectionView* _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self ImArticleOrQuestionVC];
    
    [self _createCollectionView];
    
    //当文章、问题页面的收藏按钮被点击时，就接受通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(articleQuestionLikeBtnClickedNotification:) name:kArticleQuestionLikeBtnNotification object:nil];
}

- (void)_createCollectionView {
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(kWidth, kHeight-navBarHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//滑动方向水平
    
    _collectionView = [[ArticleCollectionView alloc] initWithFrame:CGRectMake(0, navBarHeight, kWidth, kHeight-navBarHeight) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
}



//这里需要做以下事情
//01 当收藏、点赞按钮被点击时，得到当前被点击的页面类型和日期
//02 调用tool中的方法，把相关数据存储到本地，或是从本地删除
- (void)articleQuestionLikeBtnClickedNotification:(NSNotification*)notification {
    NSDictionary* info = notification.userInfo;
    BOOL isLiked = [info[@"liked"] boolValue];
    
    
    //01 得到当前显示的页码
    NSInteger index = _collectionView.contentOffset.x/kWidth;
    
    //得到相应的日期
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-index*24*60*60];
    NSString* dateStr = [formatter stringFromDate:date];
    
    
    //02 如果加入收藏
    if (isLiked) {
        [Tools saveMessage:@"like" strCt:@1 date:dateStr];
    } else {        //如果取消收藏
        [Tools deleteMessage:@"like" strCt:@1 date:dateStr];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kArticleQuestionLikeBtnNotification object:nil];
}


@end
